<?php
namespace app\index\controller\project;

class returned extends base_project
{
    public $tableName = "ProjectReturned";
    public function index()
    {
        $this->page(["project_id"=>$this->base_project_id],10,"ProjectReturned","id desc","project");
         $code_status=  model("CodeMap")->get_value("project_returned_status");
        $data["code_status"] =  array_column($code_status,"m_name","m_value");
        $this->assign($data);
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {
        $model = model('ProjectReturned');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
         
            $data["project_id"] = $this->base_project_id;
            $data["project_price"] = $post["project_price"];
            $data["plan_ratio"] = $post["plan_ratio"];
            $data["plan_price"] = $post["plan_price"];
            $data["real_price"] = $post["real_price"];
            $data["real_ratio"] = $post["real_ratio"];
            $data["plan_time"] = $post["plan_time"];
            $data["real_time"] = $post["real_time"];
            $data["status"] = $post["status"];
            $data["strategy"] = $post["strategy"];
            $data["risk"] = $post["risk"];
            $data["uid"] =$this->member_id;
            if (!empty($post["assist_users"])) 
            {
                $assist_users=implode(",", $post["assist_users"]);
                $data["assist_users"]= $assist_users;
            }

            $ProjectReturnedModel = model("ProjectReturned");
            $result = $this->operateData($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Returned/index"));
        }
        $data["code_status"] = model("CodeMap")->get_value("project_returned_status");
        $data["users"]=model("admin")->field("id,username,nickname")->select();
      $data["info"] = model("Project")->where(["id"=>$this->base_project_id])->field("title,price")->find();
      $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $ProjectReturnedModel = model('ProjectReturned');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["plan_ratio"] = $post["plan_ratio"];
            $data["plan_price"] = $post["plan_price"];
            $data["real_price"] = $post["real_price"];
            $data["real_ratio"] = $post["real_ratio"];
            $data["plan_time"] = $post["plan_time"];
            $data["real_time"] = $post["real_time"];
            $data["status"] = $post["status"];
            $data["strategy"] = $post["strategy"];
            $data["risk"] = $post["risk"];
            $data["uid"] = $this->member_id;
            if (!empty($post["assist_users"])) 
            {
                $assist_users=implode(",", $post["assist_users"]);
                $data["assist_users"]= $assist_users;
            }

            $map["id"] = $post["id"];
            $result = $this->operateData($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Returned/index"));
        }

        $id = input("id");
        $data["code_status"] = model("CodeMap")->get_value("project_returned_status");
        $data["info"] = $ProjectReturnedModel->where(["id"=>intval($id)])->with("project,user")->find();
        $data["users"]=model("admin")->field("id,username,nickname")->select();
       $user_ids=[];

        if(!empty($data["info"]["assist_users"])){
            $user_ids=explode(",",$data["info"]->assist_users);
        }
       
        $data["user_ids"]=$user_ids;
        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $result = $this->delBase(["id" => intval($id)]);
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

    public  function show(){
        $ProjectReturnedModel = model('ProjectReturned');
        $id = input("id");

        $data["info"] = $ProjectReturnedModel->where(["id"=>intval($id)])->with("project,user")->find();
        $code_status=  model("CodeMap")->get_value("project_returned_status");
        $data["code_status"] =  array_column($code_status,"m_name","m_value");
        $users="";
        if(!empty($data["info"]["assist_users"])){
            $users=model("admin")->where(["id"=>array("in",$data["info"]["assist_users"])])->field("id,username,nickname")->select()->toArray();
            $users=array_column($users,"nickname");
            $users=implode(",", $users);
        }
      
        $data["users"]=$users;
        $this->assign($data);
        return view();
    }


    public  function status(){
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["status"]=$post["status"];
           $model= model("ProjectReturned");
            $result =   $model->where(["id"=>$post["id"]])->update($data);
            if ($result) {
                $this->success("操作成功");
            }
            $this->error("操作失败");
        }
        $ProjectReturnedModel = model('ProjectReturned');
        $id = input("id");

        $data["info"] = $ProjectReturnedModel->where(["id"=>intval($id)])->with("project,user")->find();
        $data["code_status"] = model("CodeMap")->get_value("project_returned_status");
       
        $this->assign($data);
        return view();
    }

}
